import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useKeepAliveStore = defineStore('sv-keep-alive', () => {
  const keepAliveList = ref([])

  // 添加 keepAlive
  function addKeepAlive(name) {
    if (!keepAliveList.value.includes(name)) {
      keepAliveList.value.push(name)
    }
  }

  // 移除 keepAlive
  function removeKeepAlive(name) {
    keepAliveList.value = keepAliveList.value.filter((item) => item !== name)
  }

  // 设置 keepAlive
  function setKeepAlive(names = []) {
    keepAliveList.value = names
  }

  return {
    keepAliveList,
    addKeepAlive,
    removeKeepAlive,
    setKeepAlive
  }
})
